#include <iostream>
#include <string.h>
#include <sys/types.h> 
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;

struct MyFile
{
    int fileno;
};

MyFile* my_fopen(const char* filename, const char* mode)
{
    int fd = 0;
    
    if(strcmp(mode, "w") == 0)
    {
        fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);    
    }
    else if(strcmp(mode, "r") == 0)
    {
        fd = open(filename, O_RDONLY);
    }
    else if(strcmp(mode, "a") == 0)
    {
        fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
    }

    MyFile *fp = new MyFile;
    fp->fileno = fd;
    return fp;
}

int my_write(const char* ptr, MyFile* fp)
{
    return write(fp->fileno,ptr, strlen(ptr));
}

void my_close(MyFile* fp)
{
    close(fp->fileno);
}

int main()
{
    MyFile* fp = my_fopen("myfile.txt","w");

    my_write("haha",fp);

    my_close(fp);

    return 0;
}
